package com.howentech.framework;

import org.checkerframework.checker.nullness.compatqual.NullableDecl;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.List;

/**
 * @author rebort
 */

public class BasePage {

    public static WebDriver driver;
    public static String pageTitle;
    public static String pageUrl;

    /**
     * 构造方法
     */
    protected BasePage(WebDriver driver) {
        BasePage.driver = driver;
    }

    /**
     * 在文本框内输入字符
     */
    protected void input(WebElement element, String text) {
        try {
            if (element.isEnabled()) {
                element.clear();
                Logger.Output(LogType.LogTypeName.INFO,
                        "如果存在此元素，则清空并输入值: " + element.toString() + ".");
                element.sendKeys(text);
                Logger.Output(LogType.LogTypeName.INFO, "类型值为: "
                        + text + ".");
            }
        } catch (Exception e) {
            Logger.Output(LogType.LogTypeName.ERROR, e.getMessage() + ".");
            e.printStackTrace();
        }

    }

    /**
     * 点击元素，这里指点击鼠标左键
     */
    protected void click(WebElement element) {

        try {
            if (element.isEnabled()) {
                element.click();
                Logger.Output(LogType.LogTypeName.INFO,
                        "Element: " + element.toString() + " 被点击.");
            }
        } catch (Exception e) {
            Logger.Output(LogType.LogTypeName.ERROR, e.getMessage() + ".");
        }

    }

    /**
     * 在文本输入框执行清除操作
     */
    protected void clean(WebElement element) {

        try {
            if (element.isEnabled()) {
                element.clear();
                Logger.Output(LogType.LogTypeName.INFO,
                        "Element " + element.toString() + " 被清除.");
            }
        } catch (Exception e) {
            Logger.Output(LogType.LogTypeName.ERROR, e.getMessage() + ".");
        }

    }

    /**
     * 判断一个页面元素是否显示在当前页面
     */
    protected void verifyElementIsPresent(WebElement element) {

        try {
            if (element.isDisplayed()) {
                Logger.Output(LogType.LogTypeName.INFO, "此元素 "
                        + element.toString().trim() + " 存在.");

            }
        } catch (Exception e) {
            Logger.Output(LogType.LogTypeName.ERROR, e.getMessage() + ".");
        }
    }

    /**
     * 获取页面的标题
     */
    protected String getPageTitle() {

        pageTitle = driver.getTitle();
        Logger.Output(LogType.LogTypeName.INFO, "当前页面标题为 "
                + pageTitle);
        return pageTitle;
    }

    /**
     * 获取页面的url
     */
    protected String getPageUrl() {

        pageUrl = driver.getCurrentUrl();
        Logger.Output(LogType.LogTypeName.INFO, "当前页面url为 "
                + pageUrl);
        return pageUrl;
    }

    /**
     * 查找到某一元素并且给予相应的值,一般用在对文本输入框元素的定位和输入相应值
     */
    public void sendKey(By by,String value){
        driver.findElement(by).sendKeys(value);
    }

    /**
     * 查找到某元素并点击
     */
    public void elementClick(By by){
        driver.findElement(by).click();
    }

    /**
     * 先定位到相应的选择框
     *  再定位到详细的待选项
     * 一般用在定位下拉选择框中的某一个元素
     */
    public void selectElement(By by,String text) {
        Select select = new Select(driver.findElement(by));
        select.selectByVisibleText(text);
    }

    /**
     * 判断某个元素是否存在
     */
    public boolean isElementExist(By by) {
        try
        {
            Boolean bool = driver.findElement(by).isDisplayed();
            return bool;
        }
        catch(NoSuchElementException e)
        {
            return false;
        }
    }

    /**
     * 获得某元素的文本描述信息
     */
    public String getWebText(By by) {
        try {
            return driver.findElement(by).getText();
        }
        catch (NoSuchElementException e){
            return "Text does not exist!";
        }
    }

    /**
     * 在一个元素集合中通过遍历文本内容定位到相应的元素
     */

    public void clickElementContainingText(By by,String text) {
        List<WebElement> elementList = driver.findElements(by);

        for(WebElement e:elementList)
        {
            if(e.getText().contains(text))
            {
                e.click();
                break;
            }
        }
    }

    /**
     * 在一个元素集合中通过遍历文本内容获取相应的url
     */
    public String getLinkUrlContainingText(By by, String text) {
        List<WebElement> subscribeButton = driver.findElements(by);
        String url = null;

        for (WebElement e:subscribeButton) {
            if (e.getText().contains(text))
            {
                url = e.getAttribute("href");
                break;
            }
        }
        return url;
    }

    /**
     * 某些元素由于属于某个iframe的需要先定义到frame然后再在此frame里边查找该元素
     * 此方法适合定位文本链接型元素
     */
    public void frameElementClick(By by, String text) {
        WebElement element = driver.switchTo().frame(text).findElement(by);
        element.click();
    }

    /**
     * 某些元素由于属于某个iframe的需要先定义到frame然后再在此frame里边查找该元素
     * 此方法适合定位文本输入框型元素
     */
    public void frameElementSendKey(By by,String text) {
        WebElement element = driver.switchTo().frame(text).findElement(by);
        element.sendKeys(text);
    }

    // 刷新浏览器
    public static void refresh() {
        driver.navigate().refresh();
    }

    // 浏览器前进
    public static void forward() {
        driver.navigate().forward();
    }

    // 浏览器后退
    public static void back() {
        driver.navigate().back();
    }
    //findElement元素等待的封装
    public static WebElement findElement(final By by) {
        WebElement webelement = null;
        try {
            webelement = new WebDriverWait(driver, 20).until(new ExpectedCondition<WebElement>() {
                public WebElement apply(WebDriver webDriver) {
                    return driver.findElement(by);
                }
            });
        } catch (Exception e) {
            System.out.println("元素：" + by + "查找超时！！！");
            e.printStackTrace();
        }
        return webelement;
    }

    /**
     * findElements元素等待的封装
     */
    public static List<WebElement> findElements(final By by) {
        List<WebElement> WebElement = null;
        try {
            WebElement = new WebDriverWait(driver, 20).until(new ExpectedCondition<List<WebElement>>() {
                @NullableDecl
                public List<WebElement> apply(WebDriver webDriver) {
                    return driver.findElements(by);
                }
            });
        } catch (Exception e) {
            System.out.println("元素：" + by + "查找超时！！！");
            e.printStackTrace();
        }
        return WebElement;
    }




}